<?php
/**
 * 登陆操作
 **/
namespace Mama\Controller;
use Think\Controller;

class IndexController extends Controller {
	/**
	 * 用户登录操作
	 **/
	public function index(){
		$action = I('get.ac') ? (int)I('get.ac') : 0;
		if(IS_GET){
			if(session('userid')){
				if(!$action){
					//获取菜单列表
					$menulist = D('Menu')->getMenuIdListByRoleId(session('roleid'));
					$this->assign('menulist', $menulist);
					$this->display('index');
				}else{
					session('userid', null);
					session('roleid', null);
					session('nickname', null);
					session('role_name', null);
					//清空缓存
					S('shoplist_'. session('userid'), null);
					
					$this->display('login');
				}
			}else{
				$this->display('login');
			}
		}else{
			$username = I('post.uname', '');
			$password = I('post.pwds', '');
			$logintype = I('post.type', 1);
			//开始登陆
			$this->checkLogin($logintype, $username, $password);
		}
	}
	
	/**
	 * 退出登陆
	 *
	 **/
	public function logout(){
		if(IS_GET){
			S('shoplist_'. session('userid'), null);
			session('userid', null);
			session('roleid', null);
			session('nickname', null);
			$this->ajaxReturn(1, 'json');
		}else if(IS_POST){
			$this->isCheckLogin();
		}
	}

	/**
	 * 欢迎界面
	 *
	 **/
	public function welcome(){
		if(session('userid')){
			$this->display();
		}else{
			$this->redirect("index/index");
		}
	}
	
	/**
	 * 登陆方法
	 **/
	private function checkLogin($logintype, $username, $password){
		$memberDao = D('Member');
		if($logintype == 1){
			$userinfo = $memberDao->getMemberInfoByUsername($username);
		}else{
			$userinfo = $memberDao->getMemberInfoByOtherName($username);
		}
		//判断信息
		if(is_array($userinfo) && count($userinfo) && $userinfo['roleid']){
			//获取角色权限信息
			$roleinfo = D('Role')->getRoleInfo($userinfo['roleid']);
			if($userinfo['password'] == md5($password) && $userinfo['visible']){
				//保存登陆信息
				session('userid', $userinfo['userid']);
				session('roleid', $userinfo['roleid']);
				session('nickname', $userinfo['nickname']);
				session('role_name', $roleinfo['role_name']);
				//判断管理员管理的分店列表
				$shoplist = D('Shop')->getShoprofileList($userinfo['userid'], 'shop.shop_id, shop.title');
				//设置缓存名称
				$cachename = 'shoplist_'. $userinfo['userid'];
				if(count($shoplist)){
					S($cachename, $shoplist);
				}else{
					S($cachename, null);
				}
				
				$data['error'] = 2;
			}else{
				$data['error'] = 1;
				$data['message'] = '登陆失败';
			}
		}else{
			$data['error'] = 0;
			$data['message'] = '用户不存在';
		}
		
		echo json_encode($data);
		exit;
	}
	
	/**
	 * 图片上传
	 **/
	public function upload(){
		if(!empty($_FILES['file']['tmp_name'])){
		    $upload = new \Think\Upload();// 实例化上传类
		    $upload->maxSize   =     3145728 ;// 设置附件上传大小
		    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    $upload->rootPath  =     './Public/upload/'; // 设置附件上传根目录
		    $upload->savePath  =     ''; // 设置附件上传（子）目录
		    // 上传文件 
		    $info   =   $upload->upload();
		    if(!$info) {// 上传错误提示错误信息
		        $data = array('errCode' => 0, 'errMsg' => $upload->getError());
		    }else{// 上传成功
		        foreach($info as $file){
			        $newImage = $file['savepath'].$file['savename'];
			    }
				
				$data = array('errCode' => 1, 'errMsg' => $newImage);
		    }
	    }else{
	    	$data = array('errCode' => 0, 'errMsg' => '请选择上传的图片');
	    }
		
		echo json_encode($data);
	}
}

